package user;
import Book.Library;
import utils.PermissionException;
import Constant.Constant;

public class ProxyUser {
    private User realuser;
    public ProxyUser(User user){
        this.realuser=user;
    }
    public User getRealuser(){
        return realuser;
    }
    public  int display(){
        return this.realuser.display();
    }
    //--------------------------------管理员相关方法------------------------------//
    //添加书籍操作
    private void checkRealUserWhetherAdminUser(String msg) {
        if(!(this.realuser instanceof AdminUser)) {
            throw new PermissionException(msg);
        }
    }
    public void addBook() {
        System.out.println("代理类addBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限添加书籍...");
        }catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realuser)).addBook();
    }

    //更新书籍操作
    public void updateBook() {

    }

    //移除图书
    public void removeBook() {

    }

    //查看图书的借阅次数
    public void borrowCount( ) {

    }

    //查看最受欢迎的前K本书
    public void generateBook() {

    }

    //查看库存状态
    public void checkInventoryStatus() {

    }


    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {

    }

    //--------------------------------普通用户相关方法------------------------------//
    //借阅图书
    private void checkRealUserWhetherNormalUser(String exceptionMessage) {
        if(!(realuser instanceof NormalUser)){
            throw new PermissionException(exceptionMessage);
        }
    }
    public void borrowBook() {
        System.out.println("代理类borrowBook方法执行了");
        System.out.println("检查用户权限");
        if(realuser instanceof AdminUser){
            System.out.println("您没有权限借用书籍，请以普通用户的方式借阅书籍");
        }else if(realuser instanceof NormalUser){
            ((NormalUser) realuser).borrowBook();
        }

    }

    //归还图书
    public void returnBook() {
        System.out.println("代理类returnBook方法执行了");
    }


    //查看个人借阅情况
    public void viewBorrowHistory() {
        System.out.println("代理类viewBorrowHistory方法执行了");
    }
    public void handleOperation(int choice) {

        if(this.realuser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    Library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    Library.displayBooks();
                    break;
                case Constant.EXIT:
                    Library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                default:
                    break;
            }
        }else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    Library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    Library.displayBooks();
                    break;
                case Constant.EXIT:
                    Library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
            }
        }


    }
}

